package cn.chinaunicom.sdsi.gacx.workflow.elecmanage.controller;

import cn.chinaunicom.sdsi.gacx.workflow.elecmanage.entity.FlowCfgLink;
import cn.chinaunicom.sdsi.gacx.workflow.elecmanage.service.FlowCfgLinkService;

import cn.chinaunicom.sdsi.frm.base.BaseController;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.json.JSONObject;

/**
 * @author lzq
 * 2017-5-11 16:34:36
 *
 */

@Controller
@RequestMapping("flowCfgLinkController")
public class FlowCfgLinkController extends BaseController {

    private final Log log = LogFactory.getLog(FlowCfgLinkController.class);

    @Autowired
    private FlowCfgLinkService flowCfgLinkService;

    /**
     * 跳转至查询页面
     */
    @RequestMapping("/flowCfgLinkControllerMain")
    public ModelAndView flowCfgLinkControllerMain(ModelAndView mv) {
        log.debug("FlowCfgLinkController.flowCfgLinkControllerMain{}");
        mv.setViewName("sys/sysController/FlowCfgLinkList");
        return mv;
    }

    /**
     * 跳转至新增页面
     */
    @RequestMapping("/showAddPage")
    public ModelAndView showAddPage(ModelAndView mv) {
        mv.setViewName("sys/sysController/FlowCfgLinkAdd");
        return mv;
    }

    /**
     * 分页查询
     * @param start 开始条数,名称不可改变，参数为分页组件传入
     * @param length 每页大小,名称不可改变，参数为分页组件传入
     */
    @ResponseBody
    @RequestMapping("/search4PageList")
    public JSONObject search4PageList(FlowCfgLink flowCfgLink, Integer start, Integer length) {

        log.debug("FlowCfgLinkController.search4PageList{}");
        return flowCfgLinkService.search4PageList(flowCfgLink, start, length);
    }

    /**
     * 查询单条数据
     */
    @RequestMapping("/selectByPrimaryKey")
    public ModelAndView selectByPrimaryKey(FlowCfgLink flowCfgLink, ModelAndView mv) {

        log.debug("FlowCfgLinkController.selectByPrimaryKey{}");
        FlowCfgLink bean = flowCfgLinkService.selectByPrimaryKey(flowCfgLink);
        mv.addObject("bean", bean);
        mv.setViewName("sys/sysController/FlowCfgLinkUpdate");
        return mv;
    }

    /**
     * 删除单条数据
     */
    @ResponseBody
    @RequestMapping("/deleteByPrimaryKey")
    public int deleteByPrimaryKey(FlowCfgLink flowCfgLink) {
        int delnum = flowCfgLinkService.deleteByPrimaryKey(flowCfgLink);
        return delnum;
    }

    /**
     * 保存数据
     */
    @ResponseBody
    @RequestMapping("/addObject")
    public int addObject(FlowCfgLink flowCfgLink) {
        int insertnum = flowCfgLinkService.addObject(flowCfgLink);
        return insertnum;
    }

    /**
     * 修改单条数据
     */
    @ResponseBody
    @RequestMapping("/updateByPrimaryKey")
    public int updateByPrimaryKey(FlowCfgLink flowCfgLink) {
        int updatetnum = flowCfgLinkService.updateByPrimaryKey(flowCfgLink);
        return updatetnum;
    }

    /**
     * 展示下一节点处理人
     */
    @ResponseBody
    @RequestMapping("/showNextStaff")
    public HashMap showNextStaff(HttpServletRequest request, HttpServletResponse response, String flowId,String elecApplyId) {
        HashMap map = new HashMap();
        String staffName = "";
        int staffId = 0;
        if (null != flowId && flowId != "") {
            if (flowId.equals("10001001") || flowId.equals("10001031") ||flowId.equals("10002001")) {
                if (null != elecApplyId && elecApplyId != "") {
                    List<Map> staffList = flowCfgLinkService.selectStaffByELecApplyId(elecApplyId);
                    if (staffList != null && !staffList.isEmpty()) {
                        staffName = staffList.get(0).get("STAFF_NAME").toString();
                        staffId = Integer.parseInt(staffList.get(0).get("STAFF_ORG_ID").toString());
                    }
                }
            } else if (flowId.equals("10001021")||flowId.equals("10002021")) {
                // int staffId = request.getSession().getAttribute(staff_Id);
//                SysStaff staff = (SysStaff)getSessionCache(R.UserSign.SIGNIN_USER_SESSION);
//                int loginStaffId = 10002;
                List<Map> staffList = flowCfgLinkService.selectNextStaffBySessionStaffId();
                staffName = staffList.get(0).get("STAFF_NAME").toString();
                staffId = Integer.parseInt(staffList.get(0).get("STAFF_ORG_ID").toString());
            } else if (flowId.equals("999999999")) {//废弃
                List<Map> staffList = flowCfgLinkService.selectYwStaffBySessionStaffId();
                staffName = staffList.get(0).get("STAFF_NAME").toString();
                staffId = Integer.parseInt(staffList.get(0).get("STAFF_ORG_ID").toString());
            } else if (flowId.equals("10001041")||flowId.equals("10002041")) {
                List<Map> staffList = flowCfgLinkService.selectWgStaffBySessionStaffId();
                staffName = staffList.get(0).get("STAFF_NAME").toString();
                staffId = Integer.parseInt(staffList.get(0).get("STAFF_ORG_ID").toString());
            }
        }
        map.put("staffName", staffName);
        map.put("staffId", staffId);
        return map;
    }

}